#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "com.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "keypane.h"
#include "password.h"



/*
 *
 */
void SetPasswordOtherInput(HWND hWnd, BOOL bOther)
{
    HWND hWndEdit = GetDlgItem(hWnd, ID_EDIT);
    if(hWndEdit)
    {
        SetEditPassword(hWndEdit, !bOther);
    }
    int nData = GetWindowAdditionalData(hWnd);
    if(bOther)
    {
        nData |= PASSWORD_OTHER_INPUT;
    }
    else
    {
        nData &= ~PASSWORD_OTHER_INPUT;
    }
    SetWindowAdditionalData(hWnd, nData);
}
/*
 *
 */
void SetPasswordTitleColor(HWND hWnd, int color)
{
    int nData = GetWindowAdditionalData(hWnd);
    nData &= 0xFFFF00FF;
    nData |= color;
    SetWindowAdditionalData(hWnd, nData);
}
/*
*
*/
static int PasswordProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateWindow (MH_EDIT_NAME,
                "",
                WS_CHILD | WS_VISIBLE,
                ID_EDIT,
                50,
                60,
                100,
                20,
                hWnd, MH_EDIT_PASSWORD);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.top += MASTER_TITLE_TOP;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls (hWnd);
        }
        break;
    case MSG_KEYDOWN:
        //OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_PASSWORD]);
        }
        return 0;
    case MSG_COMMAND:
        {
        }
        return 0;
    case MSG_SETDATA:
        {
            int nData = GetWindowAdditionalData(hWnd);
            int ret = -1;
            int nID = LOWORD(wParam);
            HWND hWndEdit = GetDlgItem(hWnd, nID);
            const char *sc = GetWindowCaption(hWndEdit);
            if(sc)
            {
                if(nData & PASSWORD_OTHER_INPUT)
                {
                    int len = strlen(sc);
                    if(len > 0 && len < sizeof(g_edit_text))
                    {
                        g_edit_text[0] = 0;
                        strcat(g_edit_text, sc);
                        ret = 0;
                    }
                }
                else
                {
                    ret = strcmp(sc, "8");
                }
            }
            if(ret)
            {
                SetWindowCaption(hWndEdit, MW_ERROR_PASSWORD_CAPTION);
                InvalidateRect(hWndEdit, NULL, TRUE);
            }
            else
            {
                nData |= PASSWORD_OK_CLICK;
                SetWindowAdditionalData(hWnd, nData);
            }
            return ret;
        }
        break;
    case MSG_KEYPANECLOSE:
        {
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreatePasswordDialog(HWND hHosting)
{
    int x = (CX_SCREEN - CX_PASSWORD_BMP) / 2;
    int y = (CY_SCREEN - CY_PASSWORD_BMP) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_ENTER_PASSWORD_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = PasswordProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_PASSWORD_BMP;
    CreateInfo.by = y + CY_PASSWORD_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_EDIT));
    }
    return hWnd;
}
/*
 *
 */
int PasswordDoModal(HWND hWnd)
{
    int ret = 0;
    MSG Msg;
    //////////为了调试方便，去掉密码输入进入界面/////////
/*
  int nData = GetWindowAdditionalData(hWnd);
    if(nData & PASSWORD_OTHER_INPUT)
    {
        //
    }
    else
    {
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
        ret = 1;
        return ret;
    }   //*/
    /////////////////////////////////////////////////////
    HWND hHosting = GetHosting(hWnd);
    RECT rect = {0, 0, 0, 0,};
    if(hHosting)
    {
        GetWindowRect(hWnd, &rect);
        EnableWindow(hHosting, FALSE);
    }
    if (hWnd != HWND_INVALID)
    {
        ShowWindow(hWnd, SW_SHOWNORMAL);
        CreateKeyboardWindow(hWnd, rect, KEYPANE_TYPE_FILE, KEYPANE_TYPE_FILE, 0);
        while(GetMessage(&Msg, hWnd))
        {
            if(Msg.hwnd == hWnd && Msg.message == MSG_CLOSE)
            {
                ret = GetWindowAdditionalData(hWnd) & PASSWORD_OK_CLICK;
                break;
            }
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        if(hHosting)
        {
            EnableWindow(hHosting, TRUE);
            ShowWindow(hHosting, SW_SHOWNORMAL);
            SetActiveWindow(hHosting);
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
    }
    return ret;
}

